<!--
*  2021-03-15
*  领取 纱线
-->
<template>
  <div id="app" class="yarn">
    <!-- 头部导航 -->
    <van-nav-bar
      :title="title"
      ref="header"
      left-text="退出登录"
      @click-left="onClickLeft"
    >
    </van-nav-bar>
    <van-cell-group>
      <div
        v-for="(data, key) in tableData"
        :key="key"
        :title="data.groupLabel"
        class="yarn-weaving-wrapper"
        v-permissions="data.roles"
      >
        <div class="yarn-weaving-label">{{ data.groupLabel }}</div>
        <div
          v-for="(item, index) in data.value"
          :key="index"
          @click="onClickJump(item.path, item.label)"
          class="yarn-weaving-calue"
        >
          <img :src="item.src" alt="" />
          <span>{{ item.label }}</span>
        </div>
      </div>
    </van-cell-group>
  </div>
</template>

<script>
import { openWin, closeApp } from "@/utils/index";
import { removeInfo } from "@/utils/auth";
import { getWeavingPlan, getTaskAccept } from "@/api/yarn";

export default {
  name: "material",
  data() {
    return {
      title: "织造车间",
      roles: "",
      show: false,
      btnVal: "",
      btnShow: false,
      tableData: [
        // {
        //   groupLabel: "织造车间",
        //   roles: "'45_59', '45_238",
        //   value: [
        //     {
        //       label: "领料列表",
        //       src: require("./../../../assets/img/list.png"),
        //       path: "material-receive",
        //     },
        //     {
        //       label: "退料列表",
        //       src: require("./../../../assets/img/zeroize.png"),
        //       path: "material-return",
        //     },
        //   ],
        // },
        {
          groupLabel: "码单管理",
          roles: "'57_532'",
          value: [
            {
              label: "码单列表",
              src: require("./../../../assets/img/list.png"),
              path: "material-detailed",
            },
          ],
        },
      ],
    };
  },
  mounted() {
    const that = this;
    if (process.env.NODE_ENV === "production") {
      this.$nextTick(() => {
        closeApp();
      });
    } else {
      window.apiready = function () {
        closeApp();
      };
    }
  },
  methods: {
    // 退出登录
    onClickLeft() {
      this.$dialog
        .confirm({
          title: "确认退出?",
          message: "退出后需要重新登录",
          confirmButtonText: "退出",
          confirmButtonColor: "#1677FF",
          cancelButtonColor: "#1677FF",
        })
        .then(() => {
          removeInfo();
          openWin("index");
        })
        .catch(() => {
          // on cancel
        });
    },
    // 功能
    onClickRight() {
      this.show = true;
    },
    // 扫码
    onClickStart() {
      const that = this;
      const zyHmsScan = api.require('zyHmsScan');
      zyHmsScan.openDefaultView({}, ret => {
        if(ret.status) {
          let content = ret.result;
          if (!that.$validURL(content)) {
            that.$toast.fail("二位码异常");
            return false;
          }
          getWeavingPlan({ resUrl: content }, (respon) => {
            console.log(respon);
            getWeavingPlan(
                {
                  resUrl:
                      process.env.VUE_APP_YARN_URL +
                      respon[0].resUrl +
                      "/" +
                      respon[0].resId,
                },
                (res) => {
                  console.log(res);
                  if (respon[0].status === 1) {
                    // 先接单
                    getTaskAccept(
                        {
                          taskId: respon[0].taskId,
                          stepNo: respon[0].stepNo,
                        },
                        () => {
                          that.$toast({
                            type: "success",
                            message: "接单成功",
                            onClose: () => {
                              openWin("receive-detail", {
                                flowType: respon[0].flowType,
                                stepId: respon[0].id,
                                resId: respon[0].resId,
                                taskId: respon[0].taskId,
                                stepNo: respon[0].stepNo,
                                nextStepNo: respon[0].nextStepNo,
                                count: res.yarnNum,
                              });
                            },
                          });
                        }
                    );
                  } else {
                    openWin("receive-detail", {
                      flowType: respon[0].flowType,
                      stepId: respon[0].id,
                      resId: respon[0].resId,
                      taskId: respon[0].taskId,
                      stepNo: respon[0].stepNo,
                      nextStepNo: respon[0].nextStepNo,
                      count: res.yarnNum,
                    });
                  }
                }
            );
          });
        }
      });
    },
    // 点击返回
    onClickBack() {
      this.show = false;
    },
    // 点击跳转
    onClickJump(html, title) {
      this.show = false;
      openWin(html, {
        title: title,
      });
    },
  },
};
</script>

<style lang="scss">
.yarn {
  // padding-top: 22px;

  .van-cell-group {
    height: calc(100vh - 68px);
    display: flex;
    flex-direction: column;
    align-items: center;
    background: rgba(245, 245, 245, 1) !important;
    .yarn-weaving-wrapper {
      width: 351px;
      .yarn-weaving-label {
        padding: 30px 0 15px 0;
      }
      .yarn-weaving-calue {
        height: 80px;
        display: flex;
        align-items: center;
        background: #ffffff;
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.05);
        border-radius: 2px;
        margin-bottom: 20px;
        padding-left: 40px;
        span {
          font-size: 18px;
          color: #333333;
          margin-left: 20px;
        }
      }
    }
  }
  .van-hairline--top-bottom::after,
  .van-hairline-unset--top-bottom::after {
    border-width: 0;
  }
  .van-dialog {
    background: #ffffff;
    border-radius: 8px;
    .van-dialog__message--has-title {
      padding-bottom: 80px;
    }
  }
}
</style>
